﻿using CommonNet48.Encrypt;
using System;
using System.Configuration;

namespace CommonNet48.ConfigUtils
{
    /// <summary>
    /// 系统配置文件(App.config)操作工具类
    /// </summary>
    public class AppConfigHelper
    {
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        /// <param name="sConnectionName">连接字符串配置节点名称</param>
        /// <param name="bDecrypt">是否解密</param>
        /// <returns></returns>
        public static string GetConnectionString(string sConnectionName, bool bDecrypt = true)
        {
            string sConfigConString = ConfigurationManager.ConnectionStrings[sConnectionName].ConnectionString;
            if (bDecrypt)
                return DESHelper.Decrypt(sConfigConString);
            else
                return sConfigConString;
        }

        /// <summary>
        /// 更新连接字符串
        /// </summary>
        /// <param name="sConnectionName">连接配置节点名称</param>
        /// <param name="sConnectionString">连接字符串</param>
        /// <param name="bEncrypt">是否加密</param>
        public static void UpdateConnectionString(string sConnectionName, string sConnectionString, bool bEncrypt = true)
        {
            string sConfigConString = string.Empty;
            if (bEncrypt)
                sConfigConString = DESHelper.Encrypt(sConnectionString);
            else
                sConfigConString = sConnectionString;
            Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfg.ConnectionStrings.ConnectionStrings[sConnectionName].ConnectionString = sConfigConString;
            cfg.Save();
            ConfigurationManager.RefreshSection("connectionStrings");
        }
        /// <summary>
        /// 获取配置项内容
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="sAppSettingsName">配置项节点名称</param>
        /// <param name="bDecrypt">是否解密</param>
        /// <returns></returns>
        public static T GetAppSettings<T>(string sAppSettingsName, bool bDecrypt = false)
        {
            string strVal = ConfigurationManager.AppSettings[sAppSettingsName];
            if (bDecrypt)
                strVal = DESHelper.Decrypt(strVal);
            return (T)Convert.ChangeType(strVal, typeof(T));
        }

        /// <summary>
        /// 获取配置项内容
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="sAppSettingsName">配置项节点名称</param>
        /// <param name="bDecrypt">是否解密</param>
        /// <returns></returns>
        public static T GetAppSettings<T>(string sAppSettingsName, T defValue, bool bDecrypt = false)
        {
            try
            {
                string strVal = ConfigurationManager.AppSettings[sAppSettingsName];
                if (bDecrypt)
                    strVal = DESHelper.Decrypt(strVal);
                return (T)Convert.ChangeType(strVal, typeof(T));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return defValue;
            }
        }

        /// <summary>
        /// 更新配置项内容
        /// </summary>
        /// <param name="sAppSettingsName">配置节点名称</param>
        /// <param name="sValue">写入值</param>
        /// <param name="bEncrypt">是否加密</param>
        public static void UpdateAppSettings(string sAppSettingsName, string sValue, bool bEncrypt = false)
        {
            string sTemp = string.Empty;
            if (bEncrypt)
                sTemp = DESHelper.Encrypt(sValue);
            else
                sTemp = sValue;
            Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfg.AppSettings.Settings[sAppSettingsName].Value = sTemp;
            cfg.Save();
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}
